<?php

namespace application\Module;

use application\Comm\Func;
use library\Core\Db\CDbCriteria;
use application\Model\AdimageModel;
use application\Model\ProductModel;
use library\Core\Config;
use application\Model\HouseModel;
use application\Model\DesignerModel;

class Adimage
{
    /**
     * @param $pid
     * @param array $select
     * @return array
     * 根据位置获取广告
     */
    public function getList($pid, $select = ['id', 'name', 'description', 'url', 'src','position_id'])
    {
        $criteria = new CDbCriteria();
        $criteria->select($select);
        if(is_array($pid)){
        	$criteria->addInCondition('position_id', $pid);
        }else{
        	$criteria->addEqCondition('position_id', $pid);
        }
        
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);
        $criteria->addOrderDesc('orderby');
        $adimageModel = new AdimageModel();
        $data = $adimageModel->getListForArray($criteria);
        if(!empty($data)){
            foreach($data as $k => $v){
                $data[$k]['src'] = Func::imgUrl($v['src']);
            }
        }
        return $data;
    }

    
    /**
     * @param $pid
     * @param array $select
     * @return array
     * 根据广告获取对应链接产品的详细信息
     */
    public function getProduct($pid, $select = ['id','name','price','price_ori','content'])
    {
    	
    	$datalist = $this->getList($pid);
    	$res = [];
    	foreach ($datalist as $k=>$v){
    		if($v['url']){
    			preg_match("/\d+/is",$v['url'],$res[$k]);
    		}else {
    			continue;
    		}
    	}
    	
    	$criteria = new CDbCriteria();
    	$criteria->select($select);
    	$str=[];
    		
    	foreach ($res as $k =>$v){
    		$str[$k] = isset($v[0]) ? $v[0] : '';
    	}
    	
   
    	$criteria->addInCondition('id', $str);
    	$ProductModel = new ProductModel();
    	$data = $ProductModel->getListForArray($criteria);
    	$style = Config::getParamsVal('STYLE');
    	$function = Config::getParamsVal('FUNCTION');
    	//Func::pre($style);
    	if($data){
    		foreach($data as $k => $v){
    			$data[$k]['src'] = $datalist[$k]['src'];
    			$data[$k]['url'] = $datalist[$k]['url'];
    			if ($datalist[$k]['description']){
    				$data[$k]['description'] = $datalist[$k]['description'];
    			}
    			if (isset($v['style_id'])){
    				foreach ($style as $key =>$val){
    					if ($v['style_id']==$key){
    						$data[$k]['style_id'] = $val;
    					}
    				}
    				
    			}
    			
    			if (isset($v['function_id'])){
    				foreach ($function[$v['category_id']] as $key =>$val){
    					if ($v['function_id']==$key){
    						$data[$k]['function_id'] = $val;
    					}
    				}
    			}
    		
    		}
    	}
    	
    	return $data;
    }
}
